//
//  LGDiscoverViewController.m
//  dw
//
//  Created by Apple on 15/12/7.
//  Copyright © 2015年 Apple. All rights reserved.
//

#import "LGDiscoverViewController.h"
#import "LGDiscover.h"
#import "LGDiscoverCell.h"
#import "LGDiscoverViewController.h"
#import "LGwebViewController.h"
@interface LGDiscoverViewController ()

@property (nonatomic, strong) NSMutableArray *discovers;
@property (nonatomic , assign) NSInteger page;


@end



@implementation LGDiscoverViewController

/** DiscoverCellID  */
static NSString *ID  = @"DiscoverCell";

/** 有数据时候刷新 */
- (void)setDiscovers:(NSMutableArray *)discovers {
    _discovers = discovers;
    [self.tableView reloadData];
}
#pragma mark - /************** 生命周期方法 **************/
- (void)viewDidLoad {
    [super viewDidLoad];
    // 注册 cell
    [self.tableView registerClass:[LGDiscoverCell class] forCellReuseIdentifier:ID];
    
    // 直接刷新
    [self loadData];
    
    // 下拉刷新
    self.refreshControl = [[UIRefreshControl alloc] init];
    [self.refreshControl addTarget:self action:@selector(loadData) forControlEvents:UIControlEventValueChanged];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}
- (void)loadData {
    
    // 异步加载数据
    [LGDiscover DiscoverListWithSuccessBlock:^(NSArray *array) {
        self.discovers = [NSMutableArray arrayWithArray:array];
        self.page = 1;
        
    } errorBlock:^(NSError *error) {
        NSLog(@"%@",error);
    }];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self.refreshControl endRefreshing];
    });
}

#pragma mark - /************** 数据源方法 **************/

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.discovers.count;
}
//行内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LGDiscoverCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
    
    
    if (cell == nil) {
        cell = [[LGDiscoverCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    
    // 取出数据
    LGDiscover *discover = self.discovers[indexPath.row];
    
    cell.discover = discover;
    return cell;
}

#pragma -mark /***************** 代理方法 *****************/

#pragma -mark /***************** 点击cell *****************/
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    LGwebViewController *vc = [[LGwebViewController alloc] init];
    vc.discover = self.discovers[indexPath.row];
    
    [self.navigationController pushViewController:vc animated:YES];
    
    // 选中后 取消
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
}
#pragma -mark /***************** 设置行高 *****************/
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 235;
}
#pragma -mark /***************** 通过偏移量 进行上拉刷新 *****************/
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.y > (self.discovers.count - 2) * 235 - 64) {
        NSMutableDictionary *parameter = [[NSMutableDictionary alloc] init];
        parameter.dictionary =
        @{@"Appid":@"10000024",
          @"Appkey":@"e93b6295c8143f2805c2c3013390d344",
          @"act":@"lists",
          @"client_ver":@"1.2.1",
          @"mid":@"e553e2d24021fb8a156296b27e95fe3e4cf40791",
          //@"page":@"2",
          @"platform":@"iPhone9.1",
          @"yd_auth":@"d4f6ZD7td1cA6eH0SFATxSF07DKOXB%2B7gHKhfLtmV8FYgoK%2BUm4Wp%2FCaZti6tWV75B%2FtEtCn12jixPPuZvdHoYAe%2FFntpIYn2dLpN89taV3K1vhT3Q%2BBDBixLRwmKZwel%2BXn",
          @"yd_key":@"9563klroXuyAUEc61OJWViLHGR%2BCzHgbhYmyhwkZYSJ3",
          @"yd_uid":@"7014"};
        self.page++;
        [parameter setObject:[NSNumber numberWithInteger:self.page] forKey:@"page"];
        [[LGNetworkTools sharedManager] POST:@"/discover" parameters:parameter constructingBodyWithBlock:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
            NSArray *array = responseObject[@"data"];
            
            [array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                LGDiscover *discover = [LGDiscover discoverWithDict:obj];
                [self.discovers addObject:discover];
                [self.tableView reloadData];
                
            }];
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"错误%@",error);
        }];
    }
}
@end
